package com.woniuxy.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.demo.Result;
import com.woniuxy.dto.*;
import com.woniuxy.dto.commonDto.BaseDto;
import com.woniuxy.entity.Distributor;
import com.woniuxy.enums.CodeEnum;
import com.woniuxy.exception.DistributorException;
import com.woniuxy.exception.WarehouseException;
import com.woniuxy.mapper.DistributorMapper;
import com.woniuxy.param.*;
import com.woniuxy.service.DistributorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.util.PageMysqlUtil;
import com.woniuxy.util.WoniuBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 渠道商 服务实现类
 * </p>
 *
 * @author zcx, ftl, gr, rhs
 * @since 2022-01-04
 */
@Service
public class DistributorServiceImpl extends ServiceImpl<DistributorMapper, Distributor> implements DistributorService {

    @Autowired
    DistributorService distributorService;

    @Override//分页查询渠道商
    public BaseDto<DistributorDtos> getDistributors(DistributorPageParam dp) {
        QueryWrapper qw = new QueryWrapper();
        if (dp.getDistributorStatus() != null) {
            qw.eq("distributor_status", dp.getDistributorStatus());
        }
        if (!StringUtils.isEmpty(dp.getKeyword())) {
            qw.like("distributor_name", dp.getKeyword());
        }
        qw.eq("distributor_status", 0);
        BaseDto<DistributorDtos> baseDto = PageMysqlUtil.pageQuery
                (DistributorDtos.class, this, qw, new BaseParam(dp.getIndexPage(), dp.getPageSize()));

        return baseDto;
    }

    /*
     *新增
     */
    @Override
    public boolean adddistributor(DistributorAddPagram dap) {
        if (dap.getDistributorName() == null) {
            throw new DistributorException(CodeEnum.DISTRIBUTOR_SAVE_FAIL_1.getMessage()
                    , CodeEnum.DISTRIBUTOR_SAVE_FAIL_1.getCode());
        }
        if (dap.getDistributorPhone() == null) {
            throw new DistributorException(CodeEnum.DISTRIBUTOR_SAVE_FAIL_2.getMessage()
                    , CodeEnum.DISTRIBUTOR_SAVE_FAIL_2.getCode());
        }
        if (dap.getDistributorStatus() == null) {
            throw new DistributorException(CodeEnum.DISTRIBUTOR_SAVE_FAIL_3.getMessage()
                    , CodeEnum.DISTRIBUTOR_SAVE_FAIL_3.getCode());
        }

        Distributor name = getOne(new QueryWrapper<Distributor>().eq("distributor_name", dap.getDistributorName()));

        if (!StringUtils.isEmpty(name)) {
            throw new DistributorException(CodeEnum.DISTRIBUTOR_SAVE_FAIL_4.getMessage()
                    , CodeEnum.DISTRIBUTOR_SAVE_FAIL_4.getCode());
        }
        return save(WoniuBeanUtils.copyObject(dap, Distributor.class));


    }

    @Override
    public DistributorDtos findDistributorById(Integer distributorId) {
        Distributor one = getOne(new QueryWrapper<Distributor>().eq("distributor_status", 0).eq("distributor_id", distributorId));
        if (StringUtils.isEmpty(one))
            throw new DistributorException(CodeEnum.DISTRIBUTOR_FIND_FAIL.getMessage()
                    , CodeEnum.DISTRIBUTOR_FIND_FAIL.getCode());
        return WoniuBeanUtils.copyObject(one, DistributorDtos.class);
    }

    //删除
    @Override
    public boolean deldistributor(List<Integer> ids) {
        List<Distributor> list = list(new QueryWrapper<Distributor>().in("distributor_id", ids));
        if (null == list || list.size() <= 0)
            throw new DistributorException(CodeEnum.DISTRIBUTOR_DELETE_FAIL_1.getMessage()
                    , CodeEnum.DISTRIBUTOR_DELETE_FAIL_1.getCode());

        List<Distributor> collect = list.stream().filter(general -> {
            Boolean flag = !(general.getDistributorStatus() == 1);
            if (flag) general.setDistributorStatus(1);
            return flag;
        }).collect(Collectors.toList());

        if (collect.size() != list.size())
            throw new DistributorException(CodeEnum.DISTRIBUTOR_DELETE_FAIL_2.getMessage()
                    , CodeEnum.DISTRIBUTOR_DELETE_FAIL_2.getCode());

        return updateBatchById(collect);
    }

    /*
     *修改
     */
    @Override
    public boolean updatedistributor(@RequestBody DistributorupdateParam dp) {
        Distributor byId = this.getById(dp.getDistributorId());

        if (null == byId)
            throw new DistributorException(CodeEnum.DISTRIBUTOR_DELETE_FAIL_1.getMessage(), CodeEnum.DISTRIBUTOR_DELETE_FAIL_1.getCode());
        if (byId.getDistributorStatus() == 1)
            throw new DistributorException(CodeEnum.DISTRIBUTOR_DELETE_FAIL_2.getMessage(), CodeEnum.DISTRIBUTOR_DELETE_FAIL_2.getCode());

        Distributor distributor = WoniuBeanUtils.copyObject(dp, Distributor.class);
        return updateById(distributor);

    }
}
